package org.itzheng.myrxframe;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.support.multidex.MultiDex;

import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;

import org.itzheng.and.baseutils.log.LogHelper;
import org.itzheng.and.baseutils.log.processor.impl.LoggerProcessor;
import org.itzheng.and.baseutils.ui.UIUtils;
import org.itzheng.myrxframe.bugly.BuglyUpgradeListener;
import org.itzheng.myrxframe.bugly.BuglyUpgradeStateListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Title:<br>
 * Description: <br>
 *
 * @email ItZheng@ZoHo.com
 * Created by itzheng on 2019-4-15.
 */
public class App extends Application {
    private static App mApplication;

    /**
     * 获取一个实例
     *
     * @return
     */
    public static App getInstance() {
        return mApplication;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mApplication = this;
    }

    private boolean isInit = false;

    /**
     * 初始化，将所有的初始化移动到app刚启动时
     */
    public void init() {
        if (isInit) {
            return;
        }
        isInit = true;
        initUtils();
        initActivityLifecycle();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    /**
     * 设置前后台切换监听
     */
    private void initActivityLifecycle() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            int mStartActivityCount = 0;

            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {
                //Activity 启动
                mStartActivityCount++;
                if (mStartActivityCount == 1) {
                    for (OnAppStatusChangeListener listener : mStatusChanges) {
                        listener.onForeground();
                    }
                }

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {
                //Activity 停止
                mStartActivityCount--;
                if (mStartActivityCount == 0) {
                    for (OnAppStatusChangeListener listener : mStatusChanges) {
                        listener.onBackground();
                    }
                }
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });

    }

    /**
     * 初始化工具
     */
    private void initUtils() {
        UIUtils.init(this);
        LogHelper.init(new LoggerProcessor());
        initBugly();
    }

    private void initBugly() {
        Bugly.init(getApplicationContext(), "3128cfd9af", true);
        /**
         *  设置自定义升级对话框UI布局
         *  注意：因为要保持接口统一，需要用户在指定控件按照以下方式设置tag，否则会影响您的正常使用：
         *  标题：beta_title，如：android:tag="beta_title"
         *  升级信息：beta_upgrade_info  如： android:tag="beta_upgrade_info"
         *  更新属性：beta_upgrade_feature 如： android:tag="beta_upgrade_feature"
         *  取消按钮：beta_cancel_button 如：android:tag="beta_cancel_button"
         *  确定按钮：beta_confirm_button 如：android:tag="beta_confirm_button"
         *  详见layout/upgrade_dialog.xml
         */
//        Beta.upgradeDialogLayoutId = R.layout.upgrade_dialog;
        Beta.canShowApkInfo = false;
        //自己处理版本检测结果
        Beta.upgradeListener = new BuglyUpgradeListener();
        Beta.upgradeStateListener = new BuglyUpgradeStateListener();
        Beta.init(this, false);
    }

    private final List<OnAppStatusChangeListener> mStatusChanges = new ArrayList<>();

    /**
     * APP状态改变监听，前后台状态
     */
    public interface OnAppStatusChangeListener {
        /**
         * 切换到前台进程
         */
        void onForeground();

        /**
         * 切换到后台进程
         */
        void onBackground();
    }

    /**
     * 添加app前后台状态监听
     *
     * @param listener
     */
    public void addAppStatusChangeListener(OnAppStatusChangeListener listener) {
        mStatusChanges.add(listener);
    }

    /**
     * 移除前后台状态监听
     *
     * @param listener
     */
    public void removeAppStatusChangeListener(OnAppStatusChangeListener listener) {
        mStatusChanges.remove(listener);
    }
}
